package com.zjj.learn.designmodel.pattern.proxy.cglib_proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/** Cglib代理工厂
 * *** @author zjj_admin
 * *** 2022/4/10 9:59
 */
public class CglibProxyPoint implements MethodInterceptor {


    private TrainStation station = new TrainStation();

    public TrainStation getProxyObject(){

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(TrainStation.class);
        //设置回调函数
        enhancer.setCallback(this);

        TrainStation station = (TrainStation) enhancer.create();

        return station;

    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("代理服务站收取一定的服务费(cglib代理)");
        Object invoke = method.invoke(station, objects);
        return invoke;
    }
}
